package com.dongdongshop.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.dongdongshop.mapper.TbItemCatMapper;
import com.dongdongshop.mapper.TbItemMapper;
import com.dongdongshop.operator.pojo.TbItemCat;
import com.dongdongshop.operator.pojo.TbItemCatExample;
import com.dongdongshop.operator.service.ItemCatService;
import com.dongdongshop.seller.pojo.TbItem;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
@Component
public class ItemCatServiceImpl implements ItemCatService {

    @Autowired
    private TbItemCatMapper itemCatMapper;
    @Autowired
    private TbItemMapper itemMapper;

    @Override
    public List<TbItemCat> getItemCat(Long parentId) {
        List<TbItemCat> tbItemCats = itemCatMapper.getItemCat(parentId);
        return tbItemCats;
    }

    @Override
    public TbItemCat getItemCatById(Long id) {
        TbItemCat itemCat = itemCatMapper.selectByPrimaryKey(id);
        return itemCat;
    }

    @Override
    public List<TbItemCat> getTot(Long id) {

        List<TbItemCat> TbItemCats = new ArrayList<>();
        Long pid = id;
        while (true){
            if (pid == 0){
                break;
            }
            TbItemCat tots = getTots(pid);
            pid = tots.getParentId();
            TbItemCats.add(tots);
            getTots(tots.getParentId());
        }
        return TbItemCats;
    }
    public TbItemCat getTots(Long pid) {
        TbItemCat itemCat = itemCatMapper.selectByPrimaryKey(pid);
        return itemCat;
    }

    @Override
    @Transactional
    public void addItemCat(TbItemCat itemCat) {
        itemCatMapper.insertSelective(itemCat);
    }

    @Override
    @Transactional
    public void deleteItemCatByIds(Long[] ids) {
        itemCatMapper.deleteItemCatByIds(ids);
    }

    @Override
    public void updateItemCatById(TbItemCat itemCat) {
        itemCatMapper.updateByPrimaryKeySelective(itemCat);
    }

    @Override
    public List<TbItemCat> selectItemCat() {
        List<TbItemCat> tbItemCats = itemCatMapper.selectByExample(new TbItemCatExample());
        return tbItemCats;
    }

    @Override
    public List<TbItem> getItemByGoodsId(Long id) {
        List<TbItem> tbItems = itemMapper.getItemByGoodsId(id);
        return tbItems;
    }

    @Override
    public TbItemCat selectbyid(Long category1Id) {
        TbItemCat itemCat = itemCatMapper.selectByPrimaryKey(category1Id);
        return itemCat;
    }

}
